﻿using System;
using System.Threading.Tasks;

namespace awaitAndasync
{
    class Program
    {
        static Task<int> Func1(int seed)
        {
            return Task.Run(() =>
            {
                Console.WriteLine($"seed: {seed}\t Func1 is Running... ");
                Task.Delay(2000).Wait();
                return 1 + seed;
            });
        }
        static Task<int> Func2(int p1)
        {
            return Task.Run(() =>
            {
                Console.WriteLine($"p1:{p1}\t\t Func2 is Running... ");
                Task.Delay(2000).Wait();
                return 2 + p1;
            });
        }
        static Task<int> Func3(int p2)
        {
            return Task.Run(() =>
            {
                Console.WriteLine($"p2:{p2}\t\t Func3 is Running... ");
                Task.Delay(2000).Wait();
                return 3 + p2;
            });
        }
        //static void WorkFlow()
        //{
        //var t1 = Func1(100);
        //t1.Wait();
        //var t2 = Func2(t1.Result);
        //t2.Wait();
        //var t3 = Func3(t2.Result);
        //t3.Wait();
        //Console.WriteLine($"Result:{t3.Result}");
        //}

        //static void WorkFlow()
        //{
        //    var t1 = Func1(100);
        //    var w1 = t1.GetAwaiter();
        //    w1.OnCompleted(() =>
        //    {
        //        var t2 = Func2(w1.GetResult());
        //        var w2 = t2.GetAwaiter();
        //        w2.OnCompleted(() =>
        //        {
        //            var t3 = Func3(w2.GetResult());
        //            t3.Wait();

        //            Console.WriteLine($"Result:{t3.Result}");
        //        });
        //    });
        //}
        static async Task WorkFlow()
        {
            var r1 = await Func1(100);
            Console.WriteLine("Func1 end");
            var r2 = await Func2(r1);
            Console.WriteLine("Func2 end");
            var r3 = await Func3(r2);
            Console.WriteLine("Func3 end");
            Console.WriteLine($"Result:{r3}");
        }

        static async Task Main(string[] args)
        {
            Console.WriteLine("Hello async/await World!");
            await WorkFlow();
            Console.WriteLine("WorkFlow end");
        }
        
    }
}
